import type { AxiosProgressEvent } from 'axios';
import { http } from "@/api/http/request";
import { API_URLS } from "@/api/constants/api-url";

interface VerifyResponse {
  shouldUpload: boolean;
  uploadedChunks?: number[];
}

interface UploadParams {
  fileHash: string;
  fileName: string;
  fileSize?: number;
  fileType?: string;
  totalChunks?: number;

}
interface UploadConfig {
  onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  // 可以添加其他配置项
}
/**
 * 验证文件是否存在及已上传的分片
 * @param fileHash - 文件哈希值
 * @param filename - 文件名
 */
export const verifyFile = (
  fileHash: string,
  filename: string
): Promise<VerifyResponse> => {
  return http.get(API_URLS.UPLOAD_VERIFY, {
    params: { fileHash, filename },
  });
};

/**
 * 上传文件初始化
 */

export const initUpload = (params: UploadParams) => {
  console.log("上传文件初始化", params);
  return http.post(API_URLS.UPLOAD_CHUNK_INIT, params);
}

/**
 * 上传文件分片
 * @param formData - 包含分片数据的FormData
 */
export const uploadChunk = (
  formData: FormData,
  config?: {
    onUploadProgress?: (progressEvent: AxiosProgressEvent) => void;
  }
) => {
  console.log("上传文件分片", formData);

  return http.post(API_URLS.UPLOAD_CHUNK, formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
    onUploadProgress: config?.onUploadProgress,
  });
};

/**
 * 合并文件分片
 * @param params - 合并参数
 */
export const mergeFile = (params: UploadParams) => {
  console.log("合并文件分片", params);
  return http.post(API_URLS.UPLOAD_MERGE, params);
};
